package com.boatAnchorAssistant.service.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boatAnchorAssistant.pojo.entity.UserDataDictionary;
import com.boatAnchorAssistant.pojo.param.userDataDictionary.*;
import com.boatAnchorAssistant.pojo.vo.userDataDictionary.*;
import com.boatAnchorAssistant.pojo.dto.userDataDictionary.*;
import com.boatAnchorAssistant.service.repository.in.UserDataDictionaryRepository;
import com.boatAnchorAssistant.service.service.in.UserDataDictionaryService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

/**
 * 【描 述】：用户数据字典关联表服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class UserDataDictionaryServiceImpl implements UserDataDictionaryService {

    private final UserDataDictionaryRepository userDataDictionaryRepository;

    public UserDataDictionaryServiceImpl(UserDataDictionaryRepository userDataDictionaryRepository) {
        this.userDataDictionaryRepository = userDataDictionaryRepository;
    }


    /**
     * 创建用户数据字典关联表
     *
     * @param param
     */
    @Override
    public void createUserDataDictionary(CreateUserDataDictionaryParam param) {
        UserDataDictionary userDataDictionary = new UserDataDictionary();
        BeanUtils.copyProperties(param, userDataDictionary);
        Long id = IdGenerator.SNOW.generate();
        userDataDictionary.setId(id);
        userDataDictionaryRepository.create(userDataDictionary);
    }

    /**
     * 修改用户数据字典关联表
     *
     * @param param
     */
    @Override
    public void editUserDataDictionary(EditUserDataDictionaryParam param) {
        Long id = param.getId();
        UserDataDictionary userDataDictionary = userDataDictionaryRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, userDataDictionary);
        userDataDictionaryRepository.edit(userDataDictionary);
    }

    /**
     * 移除用户数据字典关联表
     *
     * @param id
     */
    @Override
    public void removeUserDataDictionary(Long id) {
        userDataDictionaryRepository.removeById(id);
    }

    /**
     * 启动/禁用用户数据字典关联表
     *
     * @param id
     */
    @Override
    public void enabledUserDataDictionary(Long id) {
        UserDataDictionary userDataDictionary = userDataDictionaryRepository.getByIdCheck(id);
        userDataDictionary.setIsDisable(!userDataDictionary.getIsDisable());
        userDataDictionaryRepository.edit(userDataDictionary);
    }

    /**
     * 获取用户数据字典关联表详情
     *
     * @param id
     * @return
     */
    @Override
    public GetUserDataDictionaryVo getUserDataDictionary(Long id) {
        UserDataDictionary userDataDictionary = userDataDictionaryRepository.getByIdCheck(id);
        GetUserDataDictionaryVo vo = new GetUserDataDictionaryVo();
        BeanUtils.copyProperties(userDataDictionary, vo);
        return vo;
    }

    /**
     * 获取用户数据字典关联表 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetUserDataDictionaryListVo> getUserDataDictionaryList(GetUserDataDictionaryListParam param) {

        SelectUserDataDictionaryDto dto = new SelectUserDataDictionaryDto();
        BeanUtils.copyProperties(param, dto);
        Page<UserDataDictionary> page = userDataDictionaryRepository.selectUserDataDictionary(dto);
        List<UserDataDictionary> userDataDictionaryList = page.getRecords();
        List<GetUserDataDictionaryListVo> list = new ArrayList<>();
        for (UserDataDictionary userDataDictionary : userDataDictionaryList) {
            GetUserDataDictionaryListVo vo = new GetUserDataDictionaryListVo();
            BeanUtils.copyProperties(userDataDictionary, vo);

            list.add(vo);
        }
        return new PageResult<>(list, page);

    }

    /**
     * 获取用户数据字典关联表下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetUserDataDictionaryDownListVo> getUserDataDictionaryDownList(GetUserDataDictionaryDownListParam param) {
        UserDataDictionary po = new UserDataDictionary();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<UserDataDictionary> userDataDictionaryList = userDataDictionaryRepository.getListByEntity(po);

        List<GetUserDataDictionaryDownListVo> list = new ArrayList<>();
        for (UserDataDictionary userDataDictionary : userDataDictionaryList) {
            GetUserDataDictionaryDownListVo vo = new GetUserDataDictionaryDownListVo();
            BeanUtils.copyProperties(userDataDictionary, vo);
            list.add(vo);
        }
        return list;
    }


}



